package com.organize.configure;

import com.autumn.exception.ExceptionUtils;
import com.autumn.util.StringUtils;
import com.autumn.util.TypeUtils;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.env.Environment;

import java.io.Serializable;

/**
 * 属性配置
 * <p>
 * </p>
 *
 * @description TODO
 * @author: 老码农
 * @create: 2020-10-17 17:05
 **/
@ConfigurationProperties(prefix = OrganizeComponentProperties.PREFIX)
@Getter
@Setter
public class OrganizeComponentProperties implements Serializable {

    private static final long serialVersionUID = 4849695039119413240L;

    /**
     * 属性前缀
     */
    public final static String PREFIX = "component.org";

    /**
     * 启用Web Api属性 key
     */
    private final static String ENABLE_WEB_API_PROPERTIES_KEY = PREFIX + ".enable-web-api";

    /**
     * 启用Web Api属性
     */
    private final static String ENABLE_WEB_API_PROPERTIES = PREFIX + ".enableWebApi";

    /**
     * 启用资源菜单插件属性 key
     */
    private final static String ENABLE_MENU_PLUGIN_PROPERTIES_KEY = PREFIX + ".enable-menu-plugin";

    /**
     * 启用资源菜单插件属性
     */
    private final static String ENABLE_MENU_PLUGIN_PROPERTIES = PREFIX + ".enableMenuPlugin";

    private final static boolean DEFAULT_ENABLE_WEB_API = true;

    private final static boolean DEFAULT_ENABLE_MENU_PLUGIN = true;

    /**
     * 获取启用 Web Api
     *
     * @param environment 环境
     * @return
     */
    public static boolean getEnableWebApi(Environment environment) {
        ExceptionUtils.checkNotNull(environment, "environment");
        String value = environment.getProperty(ENABLE_WEB_API_PROPERTIES_KEY);
        if (StringUtils.isNullOrBlank(value)) {
            value = environment.getProperty(ENABLE_WEB_API_PROPERTIES);
        }
        if (StringUtils.isNullOrBlank(value)) {
            return DEFAULT_ENABLE_WEB_API;
        }
        return TypeUtils.toConvert(Boolean.class, value);
    }

    /**
     * 获取资源菜单启用插件
     *
     * @param environment 环境
     * @return
     */
    public static boolean getEnableMenuPlugin(Environment environment) {
        ExceptionUtils.checkNotNull(environment, "environment");
        String value = environment.getProperty(ENABLE_MENU_PLUGIN_PROPERTIES_KEY);
        if (StringUtils.isNullOrBlank(value)) {
            value = environment.getProperty(ENABLE_MENU_PLUGIN_PROPERTIES);
        }
        if (StringUtils.isNullOrBlank(value)) {
            return DEFAULT_ENABLE_MENU_PLUGIN;
        }
        return TypeUtils.toConvert(Boolean.class, value);
    }


    /**
     * 是否启用 Web Api
     */
    private boolean enableWebApi = DEFAULT_ENABLE_WEB_API;

    /**
     * 是否启用资源菜单
     */
    private boolean enableMenuPlugin = DEFAULT_ENABLE_MENU_PLUGIN;

    /**
     * 菜单配置
     */
    private ResMenuConfigProperties menu = new ResMenuConfigProperties();

    /**
     * 资源菜单配置属性
     */
    @Setter
    @Getter
    public static class ResMenuConfigProperties implements Serializable {

        private static final long serialVersionUID = -5677035498681245182L;

        /**
         * 根菜单名称
         */
        private String rootMenuName = "组织管理";


    }
}
